//
//  ApiRequest.m
//  MyFrameWork
//
//  Created by eason on 16/4/25.
//  Copyright © 2016年 普惠. All rights reserved.
//

#import "ApiRequest.h"

@implementation ApiRequest

- (instancetype)init
{
    self = [super init];
    if (self) {
        _paramDic = [NSMutableDictionary new];
        _methodDic = [NSMutableDictionary new];
    }
    return self;
}

- (void)addMethod:(NSString *)method
{
    [_methodDic setObject:method forKey:@"method"];
}

- (void)addParamWithKey:(NSString *)key object:(id)object
{
    [_paramDic setObject:object forKey:key];
}

- (NSDictionary *)requestParam
{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:_methodDic forKey:@"serverid"];
    
    if (_paramDic.count != 0) {
        [dic setObject:_paramDic forKey:@"param"];
    }
    
    NSLog(@"%@",[self DataTOjsonString:dic]);
    return dic;
}

-(NSString*)DataTOjsonString:(id)object
{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object
                                                       options:kNilOptions
                                                         error:&error];
    
    if ([jsonData length] > 0 && error == nil){
        NSString * str = [[NSString alloc] initWithData:jsonData
                                               encoding:NSUTF8StringEncoding];
        return str;
    }else{
        return nil;
    }
    
    
    //    NSString *jsonString = nil;
    //    NSError *error;
    //
    //
    //
    //    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object options:kNilOptions error:&error];
    //
    //    if (! jsonData) {
    //        NSLog(@"Got an error: %@", error);
    //    } else {
    //        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    //    }
    //    return jsonString;
}

@end
